package com.example.module_home.testWebSocket

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.example.module_home.R
import org.java_websocket.client.WebSocketClient
import org.java_websocket.handshake.ServerHandshake
import java.lang.Exception
import java.net.URI

class WebsocketActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_websocket)
        initWeb()

    }

    fun initWeb(){
        //绑定用户id
        val uri = URI.create("ws://10.161.9.80:7015/api/websocket/107")
        val client = object :WebSocketClient(uri){
            override fun onOpen(handshakedata: ServerHandshake?) {
                Log.d("webSocket", "onOpen: ")
            }

            override fun onMessage(message: String?) {
                Log.d("webSocket", "onMessage 接受到消息$message: ")
            }

            override fun onClose(code: Int, reason: String?, remote: Boolean) {
                Log.d("webSocket", "onClose: ")
            }

            override fun onError(ex: Exception?) {
                Log.d("webSocket", "onError: ")
            }

        }
//        client.connectBlocking() //阻塞方法，直到链接上
        client.connect() //非阻塞


    }
}